package com.david.service.Impl;

import com.david.dao.UserInfoMapper;
import com.david.entity.UserInfo;
import com.david.service.UserInfoInterface;
import com.david.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author David
 */
@Service
public class UserInfoService implements UserInfoInterface {

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public CardBean getAllUser(Integer page, Integer limit){
        CardBean userInfoCardBean = new CardBean();

        // 开始分页
        PageHelper.startPage(page, limit);
        List<UserInfo> userInfoList = userInfoMapper.getAllUser();
        userInfoCardBean.setData(userInfoList);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList);
        userInfoCardBean.setCount((int)pageInfo.getTotal());

        // 设置返回码
        userInfoCardBean.setCode(0);
        userInfoCardBean.setMsg("获取信息成功");

        return userInfoCardBean;
    }

    @Override
    public CardBean deleteUser(String name){
        CardBean baseBean = new CardBean();
        baseBean.setCode(0);
        baseBean.setMsg("删除成功！");
        userInfoMapper.deleteUser(name);
        return baseBean;
    }

    @Override
    public CardBean addUser(UserBean userBean){
        // 设置查询条件为名称
        CardBean baseBean = new CardBean();
        UserCondition userCondition = new UserCondition();
        userCondition.setUsername(userBean.getUsername());

        if(userInfoMapper.findUser(userCondition).size() != 0){
            baseBean.setMsg("此账户已存在！");
            baseBean.setCode(1);
        }else{
            baseBean.setCode(0);
            baseBean.setMsg("新增账户成功！");
            userInfoMapper.addUser(userBean);
        }
        return baseBean;
    }

    @Override
    public CardBean findUser(UserCondition userCondition){
        // 这儿设置code为0表示查找成功，与自己设置得status不同
        CardBean cardBean = new CardBean();
        cardBean.setMsg("查找成功！");
        cardBean.setCode(0);

        // 开始分页
        PageHelper.startPage(userCondition.getPage(), userCondition.getLimit());
        List<UserInfo> userInfoList = userInfoMapper.findUser(userCondition);
        cardBean.setData(userInfoList);

        // 获取总数
        PageInfo<UserInfo> temp = new PageInfo<>(userInfoList);
        cardBean.setCount((int)temp.getTotal());

        return cardBean;
    }

    @Override
    public CardBean batchRemoveUser(List<String> names){
        CardBean baseBean = new CardBean();
        baseBean.setMsg("删除账户成功");
        baseBean.setCode(0);
        userInfoMapper.batchRemoveUser(names);
        return baseBean;
    }

    @Override
    public CardBean changeUser(UserInfo userInfo){
        CardBean baseBean = new CardBean();

        if(userInfoMapper.findByUsername(userInfo.getUsername()) == null){
            baseBean.setCode(1);
            baseBean.setMsg("账户不存在！");
        }else if ("".equals(userInfo.getUserface()) && "".equals(userInfo.getEnabled())){
            baseBean.setCode(1);
            baseBean.setMsg("授权的两者不能都为空");
        }else{
            userInfoMapper.changeUser(userInfo);
            baseBean.setMsg("更改授权成功！");
            baseBean.setCode(0);
        }
        return baseBean;
    }

}
